﻿using MS.Data.Infratructrue.Istanbul;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MS.Data.Infratructrue.Important
{
    /// <summary>
    /// 仓储基类
    /// </summary>
    public class BaseRepository<T> : IBaseRepository<T> where T : class
    {
        /// <summary>
        /// 上下文
        /// </summary>
        MyDbContext db;
        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="db">上下文</param>
        public BaseRepository(MyDbContext db)
        {
            this.db = db;
        }
        /// <summary>
        /// 获取全部
        /// </summary>
        /// <returns>返回结果</returns>
        public IQueryable<T> GetAll()
        {
            return db.Set<T>().AsQueryable();
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns>返回结果</returns>
        public async Task<int> Add(T entity)
        {
            await db.Set<T>().AddAsync(entity);
            return await db.SaveChangesAsync();
        }
        /// <summary>
        /// 更新
        /// </summary>
        /// <returns>返回结果</returns>
        public async Task<int> Update(T entity)
        {
            db.Set<T>().Update(entity);
            return await db.SaveChangesAsync();
        }
        ///// <summary>
        ///// 删除
        ///// </summary>
        ///// <param name="entity">实体</param>
        ///// <returns>返回结果</returns>
        //Task<int> Del(int Id)
        //{
        //    db.Set<T>().Remove(Id);
        //    return
        //}
        /// <summary>
        /// 查询编号
        /// </summary>
        /// <param name="Id">编号</param>
        /// <returns>返回结果</returns>
        public async Task<T> Bysty(int Id)
        {
            return await db.Set<T>().FindAsync(Id);
        }

        public int Updates(T t)
        {
            db.Set<T>().Update(t);
            return db.SaveChanges();
        }
    }
}
